package com.partner.frame.util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.partner.bean.member.Member;
import com.partner.bean.order.Order;
import com.partner.bean.order.Ordermi;
import com.partner.bean.product.Product;
import com.partner.bean.sysright.TSysuser;
import com.partner.frame.support.util.TujiPassWordEncoder;
import com.partner.service.member.MemberService;
import com.partner.service.order.OrderService;
import com.partner.service.order.OrdermiService;
import com.partner.service.product.ProductService;
import com.partner.service.sysright.SysManagerService;

/**
 * 查询成员人数 等级  工具类
 * @author Administrator
 *
 */
public class MemberUtil {
	
	/**
	 * 查询团队人数
	 * @param id
	 * @param memberService
	 * @return
	 */
	public static int getMembernum(Integer id,MemberService memberService){
		int memberNum = 0;
		if(id!=null){
			Member m = new Member();
			m.setId(id);
			List<Member> memberList = memberService.getMembernum(m);
			memberNum = memberList.size();
		}
		return memberNum;
	}
	
	/**
	 * 查询最大编号
	 * @param memberService
	 * @return
	 */
	public static Integer getMaxNum(MemberService memberService){
		Integer maxNum = memberService.getMaxNum();
		if(maxNum == null){
			maxNum = 999;
		}
		return maxNum;
	}
	
	public static void addAccount(OrdermiService ordermiService,SysManagerService sysManagerService,Integer ordermiid) {
		Ordermi ordermi = ordermiService.selectByPrimaryId(ordermiid);
//		if(ordermi.getStatus()==0){   //只处理未激活的数据
			TSysuser bean = new TSysuser();
			bean.setUsername(ordermi.getTel());
			TSysuser user = sysManagerService.getSysUserbyUsername(bean);
			if(user==null){
				bean.setCreatetime(new Date());
				bean.setPassword(TujiPassWordEncoder.MD5("123456"));
				bean.setRealname("普通用户");
				bean.setMobile(ordermi.getTel());
				bean.setSex("m");
				bean.setMemberid(ordermi.getMemberid());
				bean.setRoleid(43);
				bean.setIsuse("y");
				sysManagerService.insert(bean);
			}	
//		}
	}
	
	/**
	 * 激活账户
	 * id：待激活用户id
	 * 分成之后需要把记录添加到订单表
	 * @param id
	 * @param memberService
	 */
	public static void changeAccountAmount(Integer ordermiid,MemberService memberService,OrdermiService ordermiService,
											OrderService orderService){
		Ordermi ordermi = ordermiService.selectByPrimaryId(ordermiid);
		if(ordermi.getStatus()==0){   //只处理未激活的数据
			ordermi.setStatus(1);
//			ordermi.setDisabled(0);
//			ordermi.setActivationtime(new Date());
			Member member = memberService.selectByPrimaryId(ordermi.getMemberid());
			member.setDisabled(1);
			if(member!=null && member.getPid()!=null){
				Member m1 = memberService.selectByPrimaryId(member.getPid());//直属上级成员
				if(m1!=null){
					Member m1_1 = new Member();
					Double changeAmount = Math.floor(m1.getSaleamount()+ordermi.getSettleaccounts()*ordermi.getOnedivideinto()/100);
					int i = (new Double(changeAmount)).intValue();
					Integer saleAmount = ordermi.getSettleaccounts()*ordermi.getOnedivideinto()/100;
					m1_1.setSaleamount(i);//总积分变更
					m1_1.setId(m1.getId());
					memberService.updateByPrimaryKey(m1_1);  //直属上级成员分佣
					//把记录添加到订单表
					insertOrder(0,"开通会员返利",saleAmount,1,"一级推广",m1,orderService);
					//可提现积分变更(定时器实现)
				}
				if(m1.getPid()!=null){
					Member m2 = memberService.selectByPrimaryId(m1.getPid());//直属上上级成员
					if(m2!=null){
						Member m2_1 = new Member();
						Double changeAmount = Math.floor(m2.getSaleamount()+ordermi.getSettleaccounts()*ordermi.getTwodivideinto()/100);
						int i = (new Double(changeAmount)).intValue();
						Integer saleAmount = ordermi.getSettleaccounts()*ordermi.getTwodivideinto()/100;
						m2_1.setSaleamount(i);//
						m2_1.setId(m2.getId());
						memberService.updateByPrimaryKey(m2_1);  //直属上上级成员分佣
						//把记录添加到订单表
						insertOrder(0,"开通会员返利",saleAmount,2,"二级推广",m2,orderService);
						//可提现积分变更(定时器实现)
					}
				}
			}
			memberService.updateByPrimaryKey(member);
			ordermiService.updateByPrimaryKey(ordermi);
		}
	}
	
	/**
	 * 开账户 新增订单
	 * @param score
	 * @param type
	 * @param productName
	 * @param member
	 * @param orderService
	 */
	public static void addAccountOrder(Integer id,MemberService memberService,OrdermiService ordermiService,SysManagerService sysManagerService){
		Member member = memberService.selectByPrimaryId(id);
		TSysuser user = new TSysuser();
		if(member!=null && member.getPid()!=null){
			Member m1 = memberService.selectByPrimaryId(member.getPid());
			TSysuser bean = new TSysuser();
			bean.setUsername(m1.getTel());
			user = sysManagerService.getSysUserbyUsername(bean);
		}
		insertOrdermi(member,1,5000,ordermiService,30,10,null,null,user,member.getSysuserid());
	}
	
	/**
	 * 购买 新增订单
	 * @param score
	 * @param type
	 * @param productName
	 * @param member
	 * @param orderService
	 */
	public static void addSaleOrder(Integer memberid,
									Integer productid,
									MemberService memberService,
									OrderService orderService,
									ProductService productService,
									OrdermiService ordermiService,SysManagerService sysManagerService){
		
		Member member = memberService.selectByPrimaryId(memberid);
//		TSysuser user = sysManagerService.selectByPrimaryKey(member.getSysuserid());
		TSysuser bean = new TSysuser();
		bean.setUsername(member.getTel());
		TSysuser user = sysManagerService.getSysUserbyUsername(bean);
		Product product = productService.selectByPrimaryId(productid);
		insertOrdermi(member,2,product.getIntegral(),ordermiService,30,10,product.getId(),product.getName(),user,member.getSysuserid());
	}
	
	public static void insertOrdermi(Member member,Integer type,Integer settleaccounts,
			OrdermiService ordermiService,Integer onedivideinto,Integer twodivideinto,
			Integer productid,String productname,TSysuser user,Integer sysuserid){
		Ordermi ordermi = new Ordermi();
		ordermi.setMemberid(member.getId());
		ordermi.setType(type);
		ordermi.setStatus(0);
		ordermi.setNum(member.getNum());
		ordermi.setTel(member.getTel());
		ordermi.setPid(member.getPid());
		ordermi.setName(member.getName());
		ordermi.setSysuserid(sysuserid);
		if(user!=null){
			ordermi.setPname(user.getUsername());
			ordermi.setPtel(user.getMobile());
		}
		ordermi.setSettleaccounts(settleaccounts);
		ordermi.setCreatetime(new Date());
		ordermi.setOnedivideinto(onedivideinto);
		ordermi.setTwodivideinto(twodivideinto);
		ordermi.setProductid(productid);
		ordermi.setProductname(productname);
		ordermiService.insert(ordermi);
	}
	
	//分成
	public static void insertOrder(Integer productid,String source,Integer score,Integer type,String productName,Member member,OrderService orderService){
		Order order = new Order();
		order.setSource(source);
		order.setName(member.getName());
		order.setMemberid(member.getId());
		order.setTel(member.getTel());
		order.setPname(member.getPname());
		order.setPtel(member.getPtel());
		order.setProductname(productName);
		order.setProductid(productid);
		order.setSettleaccounts(score);
		order.setCreatetime(new Date());
		SimpleDateFormat df1 = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		String f1=df1.format(new Date());
		order.setOrdernum("NO"+f1);
		order.setFrozenintegralstatus(1);
		order.setType(type);
		order.setDisabled(0);
		orderService.insert(order);
	}
	
	
	/**
	 * 购买分成
	 * @param id：购买人id
	 * @param productId 产品id
	 * @param productService
	 * @param memberService
	 */
	public static void changeSaleAmount(Integer ordermiid,
										MemberService memberService,OrderService orderService,
										OrdermiService ordermiService) {
		Ordermi ordermi = ordermiService.selectByPrimaryId(ordermiid);
		if(ordermi.getStatus()==0){
			Member member = memberService.selectByPrimaryId(ordermi.getMemberid());
			ordermi.setStatus(1);
			if(member!=null){
				Integer saleAmount = ordermi.getSettleaccounts()*ordermi.getOnedivideinto()/100;
				Double changeAmount = Math.floor(member.getSaleamount()+ordermi.getSettleaccounts()*ordermi.getOnedivideinto()/100);
				int i = (new Double(changeAmount)).intValue();
				member.setSaleamount(i);//总积分变更
				memberService.updateByPrimaryKey(member);
				insertOrder(ordermi.getProductid(),ordermi.getProductname()+"销售",saleAmount,3,member.getName()+"购买"+ordermi.getProductname()+"一级分成",member,orderService);
				Member m1 = memberService.selectByPrimaryId(member.getPid());//直属上级成员
				if(m1!=null){
					Member m1_1 = new Member();
					Double changeAmount_1 = Math.floor(m1.getSaleamount()+ordermi.getSettleaccounts()*ordermi.getTwodivideinto()/100);
					Integer saleAmount_1 = ordermi.getSettleaccounts()*ordermi.getTwodivideinto()/100;
					int i_1 = (new Double(changeAmount_1)).intValue();
					m1_1.setSaleamount(i_1);//总积分变更
					m1_1.setId(m1.getId());
					memberService.updateByPrimaryKey(m1_1);  //直属上级成员分佣
					//把记录添加到订单表
					insertOrder(ordermi.getProductid(),ordermi.getProductname()+"销售",saleAmount_1,4,member.getName()+"购买"+ordermi.getProductname()+"二级分成",m1,orderService);
					//可提现积分变更(定时器实现)
				}
			}
			ordermiService.updateByPrimaryKey(ordermi);
		}
	}
	
	public static Integer getleval(Integer memberid,Integer num,MemberService memberService){
		Integer level = 1;
		int number = 0;
		List<Member> list = memberService.selectnum();
		for(int i=0;i<list.size();i++){
			Member member = list.get(i);
			if(member.getId()==memberid){
				number = member.getNumber();//排名
				break;
			}
		}
		//名誉总监+名誉盟主+名誉董事=108位  等级3+等级5+等级7
		Integer levelmember = memberService.selectLevelMemberCount(); //计算名誉总监+名誉盟主+名誉董事 人数
		Integer directnum = memberService.selectdirectnum(memberid);//直推人数
		if(num>=20 && directnum>=3){ // 当天使合伙人团队成员20人并直推3人后，晋升为总监
			level = 2;
		}
		if(num >=20 && directnum>=3 && number<=68 && levelmember<108){  // 当天使合伙人团队成员20人后并为前68名，晋升为名誉总监
			level = 3;
		}
		if(num>=50 && directnum>=6){  // 当团队成员50人并直推6人后，晋升为盟主
			level = 4;
		}
		if(num >=50 && directnum>=6 && number<=30  && levelmember<108){  // 当团队成员50人后并为前30名，晋升为名誉盟主
			level = 5;
		}
		if(num >= 100 && directnum>=10){  // 当团队成员100人并直推10人后，晋升为董事
			level = 6;
		}
		if(num >= 100 && directnum>=10 && number<=10  && levelmember<108){   //当团队成员100人后并为前10名，晋升为名誉董事
			level = 7;
		}
		return level;
	}
	
	
	//更新团队人数和等级
	public static void setMembernum(Integer memberid,MemberService memberService){
		Member member = memberService.selectByPrimaryId(memberid);
		List<Member> memList = new ArrayList<Member>();
		if(member!=null && member.getPid()!=null){
			Integer mid = member.getPid();
			for(int i=0;i<15;i++){
				Member m = memberService.selectByPrimaryId(mid);
				if(m!=null){
					m.setMembernum(m.getMembernum()+1);
					m.setLevel(getleval(m.getId(),m.getMembernum(),memberService));
					memList.add(m);
					mid = 0;
					if(m.getPid()!=null){
						mid = m.getPid();
					}
				}else{
					break;
				}
			}
		}
		/*if(member!=null && member.getPid()!=null){
			Member m1 = memberService.selectByPrimaryId(member.getPid()); //上面第1级
			if(m1!=null){
				m1.setMembernum(m1.getMembernum()+1);
				m1.setLevel(getleval(m1.getId(),m1.getMembernum(),memberService));
				memList.add(m1);
				if(m1.getPid()!=null){
					Member m2 = memberService.selectByPrimaryId(m1.getPid()); //上面第2级
					if(m2!=null){
						m2.setMembernum(m2.getMembernum()+1);
						m2.setLevel(getleval(m2.getId(),m2.getMembernum(),memberService));
						memList.add(m2);
						if(m2.getPid()!=null){
							Member m3 = memberService.selectByPrimaryId(m2.getPid()); //上面第3级
							if(m3!=null){
								m3.setMembernum(m3.getMembernum()+1);
								m3.setLevel(getleval(m3.getId(),m3.getMembernum(),memberService));
								memList.add(m3);
								if(m3.getPid()!=null){
									Member m4 = memberService.selectByPrimaryId(m3.getPid()); //上面第4级
									if(m4!=null){
										m4.setMembernum(m4.getMembernum()+1);
										m4.setLevel(getleval(m4.getId(),m4.getMembernum(),memberService));
										memList.add(m4);
										if(m4.getPid()!=null){
											Member m5 = memberService.selectByPrimaryId(m4.getPid()); //上面第5级
											if(m5!=null){
												m5.setMembernum(m5.getMembernum()+1);
												m5.setLevel(getleval(m5.getId(),m5.getMembernum(),memberService));
												memList.add(m5);
												if(m5.getPid()!=null){
													Member m6 = memberService.selectByPrimaryId(m5.getPid()); //上面第6级
													if(m6!=null){
														m6.setMembernum(m6.getMembernum()+1);
														m6.setLevel(getleval(m6.getId(),m6.getMembernum(),memberService));
														memList.add(m6);
														if(m6.getPid()!=null){
															Member m7 = memberService.selectByPrimaryId(m6.getPid()); //上面第7级
															if(m7!=null){
																m7.setMembernum(m7.getMembernum()+1);
																m7.setLevel(getleval(m7.getId(),m7.getMembernum(),memberService));
																memList.add(m7);
																if(m7.getPid()!=null){
																	Member m8 = memberService.selectByPrimaryId(m7.getPid()); //上面第8级
																	if(m8!=null){
																		m8.setMembernum(m8.getMembernum()+1);
																		m8.setLevel(getleval(m8.getId(),m8.getMembernum(),memberService));
																		memList.add(m8);
																		if(m8.getPid()!=null){
																			Member m9 = memberService.selectByPrimaryId(m8.getPid()); //上面第9级
																			if(m9!=null){
																				m9.setMembernum(m9.getMembernum()+1);
																				m9.setLevel(getleval(m9.getId(),m9.getMembernum(),memberService));
																				memList.add(m9);
																				if(m9.getPid()!=null){
																					Member m10 = memberService.selectByPrimaryId(m9.getPid()); //上面第10级
																					if(m10!=null){
																						m10.setMembernum(m10.getMembernum()+1);
																						m10.setLevel(getleval(m10.getId(),m10.getMembernum(),memberService));
																						memList.add(m10);
																						if(m10.getPid()!=null){
																							Member m11 = memberService.selectByPrimaryId(m10.getPid()); //上面第11级
																							if(m11!=null){
																								m11.setMembernum(m11.getMembernum()+1);
																								m11.setLevel(getleval(m11.getId(),m11.getMembernum(),memberService));
																								memList.add(m11);
																								if(m11.getPid()!=null){
																									Member m12 = memberService.selectByPrimaryId(m11.getPid()); //上面第12级
																									if(m12!=null){
																										m12.setMembernum(m12.getMembernum()+1);
																										m12.setLevel(getleval(m12.getId(),m12.getMembernum(),memberService));
																										memList.add(m12);
																										if(m12.getPid()!=null){
																											Member m13 = memberService.selectByPrimaryId(m12.getPid()); //上面第13级
																											if(m13!=null){
																												m13.setMembernum(m13.getMembernum()+1);
																												m13.setLevel(getleval(m13.getId(),m13.getMembernum(),memberService));
																												memList.add(m13);
																												if(m13.getPid()!=null){
																													Member m14 = memberService.selectByPrimaryId(m13.getPid()); //上面第14级
																													if(m14!=null){
																														m14.setMembernum(m14.getMembernum()+1);
																														m14.setLevel(getleval(m14.getId(),m14.getMembernum(),memberService));
																														memList.add(m14);
																														if(m14.getPid()!=null){
																															Member m15 = memberService.selectByPrimaryId(m14.getPid()); //上面第15级
																															if(m15!=null){
																																m15.setMembernum(m15.getMembernum()+1);
																																m15.setLevel(getleval(m15.getId(),m15.getMembernum(),memberService));
																																memList.add(m15);
																															}
																														}
																													}
																												}
																											}
																										}
																									}
																								}
																							}
																						}
																					}
																				}
																			}
																		}
																	}
																}
															}
														}
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}*/
		//批量更新团队人数
		if(memList.size()>0){
			memberService.updateBatch(memList);
		}
		
	}
}
